package com.campus.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class JWTUtil {
    @Value("${twt.jwt-sing-name}")
    private String jwtSingName;
    public String creatJWT(Integer userId){
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        String jwt= Jwts.builder()
                .setClaims(map)
                .signWith(SignatureAlgorithm.HS256,jwtSingName)
                //一周过期
                .setExpiration(new Date(System.currentTimeMillis()+3600*7*24*1000))
                .compact();
        return jwt;
    }
    public Claims parserJwt(String jwts){
        return Jwts.parser()
                .setSigningKey(jwtSingName)
                .parseClaimsJws(jwts)
                .getBody();

    }
}
